/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core;

import com.sun.lwuit.Form;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import network.Connection;
import network.PacketBuilder;
import view.MessageBox;
import view.ProgressForm;

/**
 *
 * @author Kvest
 */
public class ScreenEngine
{
    private static final int SLEEP_TIME = 150;
    
    private static ScreenEngine engine;
    private static final Object lockObject = new Object();

    private final Vector queue;

    private Form.OnLongKeyPress onLongKeyPressListener;
    
    public static ScreenEngine getEngine()
    {
        synchronized(lockObject)
        {
            if (engine == null)
            {
                engine = new ScreenEngine();
            }
        }

        return engine;
    }

    private ScreenEngine()
    {
        queue = new Vector();
        
        onLongKeyPressListener = new Form.OnLongKeyPress() {
            public void longKeyPress(int keyCode) {
                if (keyCode == Canvas.KEY_NUM5){
                    //Отправляем запрос тревоги
                    byte[] data = PacketBuilder.createAlertRequest(DriverManager.getManager().getDriverID());
                    Connection.getConnection().send(data);
                }
            }
        };
    }
    
    public void push(Form form)
    {
        synchronized(queue)
        {
            queue.addElement(form);
        }
        MessageBox.hide();
        form.setOnLongKeyPressListener(onLongKeyPressListener);
        form.show();
    }

    public void popTo(int index)
    {
        synchronized(queue)
        {
            for (int i = queue.size() - 1; i > index; --i)
            {
                queue.removeElementAt(i);
            }
            
            if (queue.size() > 0)
            {
                MessageBox.hide();
                ((Form)queue.elementAt(queue.size() - 1)).show();
                
                //Костыль, иначе не предыдущая форма
                try
                {
                    Thread.sleep(SLEEP_TIME);
                }catch(Exception ex){}
            }
        }
    }
    
    public void popProgressForm(boolean showPreviouse)
    {
        synchronized(queue)
        {
            boolean deleted = false;
            for (int i = queue.size() - 1; i >= 0; --i)
            {
                if (queue.elementAt(i) instanceof ProgressForm)
                {
                    queue.removeElementAt(i);
                    deleted = true;
                }
            }
            
            if (deleted && showPreviouse)
            {
                MessageBox.hide();
                ((Form)queue.elementAt(queue.size() - 1)).show();

                //Костыль, иначе не предыдущая форма
                try
                {
                    Thread.sleep(SLEEP_TIME);
                }catch(Exception ex){}
            }
        }
    }
    
    public Form pop(boolean showPreviouse)
    {
        synchronized(queue)
        {
            if (queue.size() == 0)
            {
                return null;
            }
            else
            {
                Form result = (Form)queue.elementAt(queue.size() - 1);// lastElement();
                queue.removeElement(result);
                if (showPreviouse)
                {
                    MessageBox.hide();
                    ((Form)queue.elementAt(queue.size() - 1)).show();

                    //Костыль, иначе не предыдущая форма
                    try
                    {
                        Thread.sleep(SLEEP_TIME);
                    }catch(Exception ex){}
                }

                return result;
            }
        }
    }

    public void showCurrent()
    {
        synchronized(queue)
        {   
            if (queue.size() > 0)
            {
                MessageBox.hide();
                ((Form)queue.elementAt(queue.size() - 1)).show();

                //Костыль, иначе не отобразит эту форму
                try
                {
                    Thread.sleep(SLEEP_TIME);
                }catch(Exception ex){}
            }
        }
    }

    public Form getBeforeCurrent()
    {
        synchronized(queue)
        {
            if (queue.size() < 2)
            {
                return null;
            }
            else
            {
                return (Form)queue.elementAt(queue.size() - 2);
            }
        }
    }

    public Form getForm(int index)
    {
        synchronized(queue)
        {
            if (queue.size() == 0 || index >= queue.size())
            {
                return null;
            }
            else
            {
                return (Form)queue.elementAt(index);
            }
        }
    }
    
    public Form getCurrent()
    {
        synchronized(queue)
        {
            if (queue.size() == 0)
            {
                return null;
            }
            else
            {
                return (Form)queue.elementAt(queue.size() - 1);
            }
        }
    }
    
    public int getScreensCount()
    {
        synchronized(queue)
        {
            return queue.size();
        }
    }

//    private void printState(String methodName)
//    {
//        synchronized(queue)
//        {
//            System.out.println("STATE: count=" + queue.size() + ", current=" + queue.elementAt(queue.size() - 1).getClass().getName() + ", methodName=" + methodName);
//        }
//    }
}
